<template>
	<view class="dialog" @click="clkclose" :style="{ 'height': setHeight + 'px' }">
		<view @click.stop="clknone" class="dlgcon" style="width: 600px;">
			<view class="comtitle">{{htname}}-明细</view>
			<view class="line">
				<view style="width: 46%;">
					<view class="line">开票</view>
					<view class="line" v-for="(item,index) of lst.filter(rs=>rs.amount>0)" :key="index">
						<text class="lncol">{{item.tm}}</text><text class="lncol">{{item.amount/100}}</text>
						<view class="lineopt">
							<button @click="clkkp(item,1)">编辑</button>
							<button @click="clkkpdel(item.id)">删除</button>
						</view>
					</view>
				</view>
				<view style="width: 46%;">
					<view class="line">付款</view>
					<view class="line" v-for="(item,index) of lst.filter(rs=>rs.amount<0)" :key="index">
						<text class="lncol">{{item.tm}}</text><text class="lncol">{{-item.amount/100}}</text>
						<view class="lineopt">
							<button @click="clkkp(item,2)">编辑</button>
							<button @click="clkkpdel(item.id)">删除</button>
						</view>
					</view>
				</view>
			</view>
			<view class="line">
				<view>汇总 : {{kamount/100}}</view>
				<view>汇总 : {{famount/100}}</view>
			</view>
			<view class="line">
				<view> </view>
				<view>未付款：{{(kamount-famount)/100}}</view>
			</view>
		</view>
		<fkedit v-if="fkshow" :kpid="kpid" v-on:close="fkclose"></fkedit>
		<kpedit v-if="kpshow" :htid="htid" :kpid="kpid" :tp="kptp" :htname="htname" v-on:close="kpclose"></kpedit>
	</view>
</template>

<script>
	import fkedit from "@/components/fkedit.vue"
	import kpedit from "@/components/kpedit.vue"
	export default {
		name:"kplist",
		components:{fkedit,kpedit},
		props:['htid','htname'], //合同ID
		data() {
			return {
				lst:[], //开票记录
				setHeight:200,
				fkshow:false,
				fkid:0,
				kpshow:false,
				famount:0,
				kamount:0,
				kpid:0,
				kptp:1
			};
		},
		mounted(){
			let self = this;
			uni.getSystemInfo({
			    success: function (res) {
					self.setHeight = res.screenHeight;
			        //console.log("show getSystemInfo",res);
			    }
			});
			this.ld();
		},
		methods:{
			ld(){ //加载数据
				let self = this;
				uni.request({
					url:this.apibase + "kplist?htid=" + this.htid,
					success: (res) => {
						self.lst = res.data;
						self.famount = 0;
						self.kamount = 0;
						for(let op of self.lst){
							if(op.amount > 0) self.kamount += op.amount;
							else self.famount += -op.amount;
						}
					}
				});
			},
			clkfk(e){
				//console.log(e)
				this.fkshow = true;
				this.kpid = e.id;
			},
			clkclose(){
				console.log('clkclose');
				this.$emit("close");
			},
			clknone(e){
				console.log('clknone');
			},
			fkclose(){
				this.fkshow = false;
			},
			clkkp(e,tp){
				//console.log(e)
				this.kpshow = true;
				this.kpid = e.id;
				this.kptp = tp;
			},
			kpclose(){
				this.kpshow = false;
				this.ld();
			},
			clkkpdel(id){
				let self = this;
				uni.request({
					url:this.apibase + "kpdel?kpid=" + id,
					success: (res) => {
						if(res.data.code){
							uni.showToast({
								title:res.data.msg
							})
						}else{
							self.ld();
						}
					}
				});
			}
		}
	}
</script>

<style>

</style>
